package top.jlzhou.config;


import javax.servlet.http.HttpServletRequest;

import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.support.RequestContext;

import lombok.extern.slf4j.Slf4j;
import top.jlzhou.CodeException;
import top.jlzhou.R;



/**
 * 全局异常处理
 * @author J.L.Zhou
 *
 */
@Slf4j
public class GlobalHandlerExceptionResolver  {

	
	@ExceptionHandler(CodeException.class)
	@ResponseBody
	public R handler(CodeException ex,HttpServletRequest request){
		R r;
		if(ex.getMessage()==null){
			RequestContext rc = new RequestContext(request);
			ex.setMessage(rc.getMessage("code."+ex.getCode()));
		}

		r = R.error(ex.getCode(), ex.getMessage());
		log.info("web全局异常", ex);
		return r;
	}

	

}
